package com.macro.mall.controller;

import com.google.common.collect.Maps;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.dto.*;
import com.macro.mall.model.*;
import com.macro.mall.service.CaseStyleService;
import com.macro.mall.service.DesignArticleService;
import com.macro.mall.service.PlatFromService;
import com.macro.mall.service.TenantService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 平台管理controller
 */
@RestController
@Api(tags = "PlatFormController", description = "平台管理后台")
@RequestMapping("/platForm")
public class PlatFormController {

    @Autowired
    private PlatFromService platFromService;

    @Autowired
    private TenantService tenantService;

    @Autowired
    private CaseStyleService caseStyleService;

    @Autowired
    private DesignArticleService articleService;

    /**
     *  查询商家列表
     *
     * @param dto 入参
     * @return List
     */
    @ApiOperation(value = "查询商家列表")
    @RequestMapping(value = "/findTenantList", method = RequestMethod.POST)
    public CommonResult findTenantList(@RequestBody TenantDto dto){
        Integer status = dto.getStatus();
        int tenantCount = platFromService.findTenantCount(status);
        Integer curPage = dto.getPageNo();
        Integer pageSize = dto.getPageSize();
        List<TenantEntity> tenantList = null;
        if(tenantCount > 0){
            tenantList = platFromService.findTenantList(status, curPage, pageSize);
        }

        Map<Object, Object> map = Maps.newHashMap();
        map.put("list", tenantList);
        map.put("pageNum", curPage);
        map.put("pageSize", pageSize);
        map.put("total", tenantCount);
        Integer totalPage = (tenantCount+1)/pageSize + 1;
        map.put("totalPage", totalPage);
        return CommonResult.success(map);
    }


    /**
     *  查询商家列表
     *
     * @param dto 入参
     * @return List
     */
    @ApiOperation(value = "查询申请商家列表")
    @RequestMapping(value = "/findApplyTenantList", method = RequestMethod.POST)
    public CommonResult findApplyTenantList(@RequestBody TenantDto dto){
        int tenantCount = platFromService.findTenantCount(0);
        Integer curPage = dto.getPageNo();
        Integer pageSize = dto.getPageSize();
        List<TenantEntity> tenantList = null;
        if(tenantCount > 0){
            tenantList = platFromService.findTenantList(0, curPage, pageSize);
        }

        Map<Object, Object> map = Maps.newHashMap();
        map.put("list", tenantList);
        map.put("pageNum", curPage);
        map.put("pageSize", pageSize);
        map.put("total", tenantCount);
        Integer totalPage = (tenantCount+1)/pageSize + 1;
        map.put("totalPage", totalPage);
        return CommonResult.success(map);
    }

    @ApiOperation(value = "根据ID查询商家信息info")
    @RequestMapping(value = "/findTenantInfoById/{tenantId}", method = RequestMethod.GET)
    public CommonResult findTenantInfoById(@PathVariable("tenantId") Long tenantId){
        TenantEntity tenantInfo = tenantService.findTenantInfoById(tenantId);
        return CommonResult.success(tenantInfo);
    }

    @ApiOperation(value = "添加商家")
    @RequestMapping(value = "/addTenant", method = RequestMethod.POST)
    public CommonResult addTenant(@RequestBody TenantInfoDto tenantInfoDto){
        platFromService.addTenant(tenantInfoDto);
        return CommonResult.success(null);
    }

    @ApiOperation(value = "审核商家")
    @RequestMapping(value = "/examineTenant/{id}", method = RequestMethod.GET)
    public CommonResult examineTenant(@PathVariable("id") Long id,
                                      @RequestParam(value = "status", defaultValue = "1") Integer status){
        platFromService.examineTenant(status, id);
        return CommonResult.success(null);
    }

    @ApiOperation(value = "勾选精选商家")
    @RequestMapping(value = "/recommendTenant/{id}", method = RequestMethod.GET)
    public CommonResult recommendTenant(@PathVariable("id") Long id,
                                        @RequestParam(value = "isRecommend", defaultValue = "1") Integer isRecommend){
        platFromService.recommendTenant(isRecommend, id);
        return CommonResult.success(null);
    }

    @ApiOperation(value = "删除商家")
    @RequestMapping(value = "/deleteTenant/{id}", method = RequestMethod.GET)
    public CommonResult deleteTenant(@PathVariable("id") Long id){
        platFromService.deleteTenant(id);
        return CommonResult.success(null);
    }

    @ApiOperation(value = "查询首页广告")
    @RequestMapping(value = "/findBannerPics", method = RequestMethod.POST)
    public CommonResult findBannerPics(@RequestBody PageDto pageDto){
        int bannerPicsNum = platFromService.findBannerPicsNum();
        List<BannerPicEntity> bannerPicList = null;
        Integer pageNo = pageDto.getPageNo();
        Integer pageSize = pageDto.getPageSize();
        if(bannerPicsNum > 0){
            bannerPicList = platFromService.findBannerPicList(pageNo, pageSize);
        }

        Map<Object, Object> map = Maps.newHashMap();
        map.put("list", bannerPicList);
        map.put("pageNum", pageNo);
        map.put("pageSize", pageSize);
        map.put("total", bannerPicsNum);
        Integer totalPage = (bannerPicsNum+1)/pageSize + 1;
        map.put("totalPage", totalPage);
        return CommonResult.success(map);
    }


    @ApiOperation(value = "查询首页广告详细信息")
    @RequestMapping(value = "/findBannerPicsInfo/{id}", method = RequestMethod.GET)
    public CommonResult findBannerPicsInfo(@PathVariable("id") Long id){
        BannerPicEntity bannerPicEntity = platFromService.findBannerPicsInfo(id);
        return CommonResult.success(bannerPicEntity);
    }

    @ApiOperation(value = "添加首页banner")
    @RequestMapping(value = "/addBannerPics", method = RequestMethod.POST)
    public CommonResult addBannerPics(@RequestBody BannerPicDto bannerPicDto){
        platFromService.addBannerPics(bannerPicDto);
        return CommonResult.success(null);
    }

    @ApiOperation(value = "修改首页banner")
    @RequestMapping(value = "/updateBannerPics", method = RequestMethod.POST)
    public CommonResult updateBannerPics(@RequestBody BannerPicDto bannerPicDto){
        platFromService.updateBannerPics(bannerPicDto);
        return CommonResult.success(null);
    }

    @ApiOperation(value = "删除首页banner")
    @RequestMapping(value = "/deleteBannerPics/{bannerId}", method = RequestMethod.GET)
    public CommonResult deleteBannerPics(@PathVariable("bannerId") Long bannerId){
        platFromService.deleteBannerPics(bannerId);
        return CommonResult.success(null);
    }

    @ApiOperation(value = "勾选精选案例")
    @RequestMapping(value = "/recommendCase/{id}", method = RequestMethod.GET)
    public CommonResult recommendCase(@PathVariable("id") Long id,
                                      @RequestParam(value = "isRecommend", defaultValue = "1") Integer isRecommend){
        platFromService.recommendCase(isRecommend,id);
        return CommonResult.success(null);
    }

    @ApiOperation(value = "平台删除案例")
    @RequestMapping(value = "/deleteCase/{id}", method = RequestMethod.GET)
    public CommonResult deleteCase(@PathVariable("id") Long id){
        platFromService.deleteCase(id);
        return CommonResult.success(null);
    }

    @ApiOperation(value = "查询所有案例")
    @RequestMapping(value = "/findAllCase", method = RequestMethod.POST)
    public CommonResult findAllCase(@RequestBody PageDto pageDto){
        int caseCount = platFromService.findAllCaseNum();
        Integer pageSize = pageDto.getPageSize();
        Integer curPage = pageDto.getPageNo();
        List<CaseEntity> caseList = null;
        if(caseCount > 0){
            caseList = platFromService.findAllCaseList(pageSize, curPage);
        }
        Map<Object, Object> map = Maps.newHashMap();
        map.put("list", caseList);
        map.put("pageNum", curPage);
        map.put("pageSize", pageSize);
        map.put("total", caseCount);
        Integer totalPage = (caseCount+1)/pageSize + 1;
        map.put("totalPage", totalPage);
        return CommonResult.success(map);
    }

    @ApiOperation(value = "查询小区列表")
    @RequestMapping(value = "/findDesiginArea", method = RequestMethod.POST)
    public CommonResult findDesiginArea(@RequestBody PageDto pageDto){
        int areaCount = platFromService.findDesiginAreaCount();
        Integer pageSize = pageDto.getPageSize();
        Integer curPage = pageDto.getPageNo();
        List<DesiginAreaDto> desiginArea = null;
        if(areaCount > 0){
             desiginArea = platFromService.findDesiginArea(curPage, pageSize);
        }
        Map<Object, Object> map = Maps.newHashMap();
        map.put("list", desiginArea);
        map.put("pageNum", curPage);
        map.put("pageSize", pageSize);
        map.put("total", areaCount);
        Integer totalPage = (areaCount+1)/pageSize + 1;
        map.put("totalPage", totalPage);
        return CommonResult.success(map);
    }

    @ApiOperation(value = "新增小区列表")
    @RequestMapping(value = "/addDesiginArea", method = RequestMethod.POST)
    public CommonResult addDesiginArea(@RequestBody DesiginAreaDto desiginAreaDto){
        int areaNameCount = platFromService.findAreaNameCount(desiginAreaDto);
        if(areaNameCount > 0){
            return CommonResult.failed("已经有该小区名称,请重新输入！");
        }
        platFromService.addDesiginArea(desiginAreaDto);
        return CommonResult.success(null);
    }

    @ApiOperation(value = "删除小区列表")
    @RequestMapping(value = "/deleteDesiginArea/{areaId}", method = RequestMethod.GET)
    public CommonResult findDesiginArea(@PathVariable("areaId") Long areaId){
        platFromService.deleteDesiginArea(areaId);
        return CommonResult.success(null);
    }

    @ApiOperation(value = "修改小区列表")
    @RequestMapping(value = "/updateDesiginArea", method = RequestMethod.POST)
    public CommonResult updateDesiginArea(@RequestBody DesiginAreaDto desiginAreaDto){
        int areaNameCount = platFromService.findAreaNameCount(desiginAreaDto);
        if(areaNameCount > 0){
            return CommonResult.failed("已经有该小区名称,请重新输入！");
        }
        platFromService.updateDesiginArea(desiginAreaDto);
        return CommonResult.success(null);
    }

    @ApiOperation(value = "查询未使用的账号")
    @RequestMapping(value = "/findNotUserNoList", method = RequestMethod.GET)
    public CommonResult findNotUserNoList(){
        return CommonResult.success(platFromService.findUserList());
    }

    @ApiOperation(value = "获取案例风格列表", notes = "获取案例风格列表", httpMethod = "GET")
    @GetMapping("/styles")
    public CommonResult getCaseStyleList() {
        return CommonResult.success(caseStyleService.getCaseStyleList());
    }


    @PostMapping("/findCaseStyleList")
    @ApiOperation(value = "分页获取案例风格列表", notes = "分页获取案例风格列表", httpMethod = "POST")
    public CommonResult findCaseStyleList(@RequestBody PageDto pageDto) {

        int caseCount = caseStyleService.findCaseStyleNum();
        Integer pageSize = pageDto.getPageSize();
        Integer curPage = pageDto.getPageNo();
        List<CaseStyle> caseStyleList = null;
        if(caseCount > 0){
           caseStyleList = caseStyleService.findCaseStyleList(curPage, pageSize);
        }
        Map<Object, Object> map = Maps.newHashMap();
        map.put("list", caseStyleList);
        map.put("pageNum", curPage);
        map.put("pageSize", pageSize);
        map.put("total", caseCount);
        Integer totalPage = (caseCount+1)/pageSize + 1;
        map.put("totalPage", totalPage);
        return CommonResult.success(map);
    }

    @GetMapping("/deleteCaseStyle/{id}")
    @ApiOperation(value = "删除风格byId", notes = "删除风格byId", httpMethod = "GET")
    public CommonResult deleteCaseStyle(@PathVariable("id") Integer id){
        caseStyleService.deleteByPrimaryKey(id);
        return CommonResult.success(null);
    }

    @PostMapping("/addCaseStyle")
    @ApiOperation(value = "平台新增风格", notes = "平台新增风格", httpMethod = "POST")
    public CommonResult addCaseStyle(@RequestBody CaseStyle record){
        // 查询新增名称是否重复
        int styleNameNum = caseStyleService.findStyleNameNum(record.getName(), null);
        if(styleNameNum > 0){
            return CommonResult.failed("风格名称重复。");
        }
        caseStyleService.insert(record);
        return CommonResult.success(null);
    }

    @PostMapping("/updateCaseStyle")
    @ApiOperation(value = "平台修改风格", notes = "平台新增风格", httpMethod = "POST")
    public CommonResult updateCaseStyle(@RequestBody CaseStyle record){
        // 查询新增名称是否重复
        int styleNameNum = caseStyleService.findStyleNameNum(record.getName(), record.getId());
        if(styleNameNum > 0){
            return CommonResult.failed("风格名称重复。");
        }
        caseStyleService.updateByPrimaryKey(record);
        return CommonResult.success(null);
    }

    @PostMapping("/findDesignArticle")
    @ApiOperation(value = "分页获取文章内容", notes = "分页获取文章内容", httpMethod = "POST")
    public CommonResult findDesignArticle(@RequestBody PageDto pageDto) {
        int articleNum = articleService.findArticleNum();
        Integer pageSize = pageDto.getPageSize();
        Integer curPage = pageDto.getPageNo();
        List<DesignArticleEntity> articleList = null;
        if(articleNum > 0){
           articleList = articleService.findArticleList(curPage, pageSize);
        }
        Map<Object, Object> map = Maps.newHashMap();
        map.put("list", articleList);
        map.put("pageNum", curPage);
        map.put("pageSize", pageSize);
        map.put("total", articleNum);
        Integer totalPage = (articleNum+1)/pageSize + 1;
        map.put("totalPage", totalPage);
        return CommonResult.success(map);
    }

    @GetMapping("/findDesignArticleById/{id}")
    @ApiOperation(value = "通过id获取文章内容", notes = "通过id获取文章内容", httpMethod = "GET")
    public CommonResult findDesignArticleById(@PathVariable("id") Integer id) {
        DesignArticleEntity articleList =  articleService.findArticleEntity(id);
        return CommonResult.success(articleList);
    }

    @PostMapping("/updateDesignArticle")
    @ApiOperation(value = "修改文章内容", notes = "修改文章内容", httpMethod = "POST")
    public CommonResult updateDesignArticle(@RequestBody DesignArticleDto dto) {
        articleService.updateDesignArticle(dto);
        return CommonResult.success(null);
    }

    @PostMapping("/saveDesignArticle")
    @ApiOperation(value = "新增文章内容", notes = "新增文章内容", httpMethod = "POST")
    public CommonResult saveDesignArticle(@RequestBody DesignArticleDto dto) {
        articleService.addDesignArticle(dto);
        return CommonResult.success(null);
    }

    @GetMapping("/deleteDesignArticle/{id}")
    @ApiOperation(value = "删除文章内容", notes = "删除文章内容", httpMethod = "GET")
    public CommonResult deleteDesignArticle(@PathVariable("id") Integer id) {
        articleService.deleteDesignArticle(id);
        return CommonResult.success(null);
    }
}
